package com.lehecai.core.lottery.fetcher.lotterydraw.impl.worker;

import com.lehecai.core.lottery.LotteryType;
import com.lehecai.core.lottery.fetcher.lotterydraw.LotteryDraw;

import java.util.HashMap;
import java.util.Map;

/**
 * 高频开奖页面Qihoo抓取结果
 * @author He Wang
 *
 */
public abstract class FrequentLotteryDrawFetchWorkerQihoo extends AbstractLotteryDrawFetchWorker {
	
	private static Map<LotteryType, String> lotteryTypeMap = new HashMap<LotteryType, String>();
	
	public static final String TR_REG = "<table[.\\s\\S]*?>[.\\s\\S]*?</table>";
	
	private static final String URL_DEFAULT = "http://cp.360.cn/int/getkpkjcode/?lotid=%s&issue=%s&r=%s";

	static {
		//快乐8
		lotteryTypeMap.put(LotteryType.BJKL8, "265108");
		//重庆时时彩-老时时彩
		lotteryTypeMap.put(LotteryType.CQSSC, "255401");
		//新快3
		lotteryTypeMap.put(LotteryType.JLK3, "258203");
		//江西时时彩
		lotteryTypeMap.put(LotteryType.JXSSC, "258001");
		//老11选5
		lotteryTypeMap.put(LotteryType.SD11X5, "166406");
		//11选5（江西）
		lotteryTypeMap.put(LotteryType.JX11X5, "168009");
		//11选5（广东）
		lotteryTypeMap.put(LotteryType.GD11X5, "165707");
        //上海11选5
        lotteryTypeMap.put(LotteryType.SH11X5, "165207");
        //黑龙江11选5
        lotteryTypeMap.put(LotteryType.HLJ11X5, "166507");
        //江苏快3
        lotteryTypeMap.put(LotteryType.JSK3, "255903");
        //广西快3
        lotteryTypeMap.put(LotteryType.GXK3, "255803");
	}

	public FrequentLotteryDrawFetchWorkerQihoo(LotteryType lotteryType) {
		super(lotteryType);
	}
	
	@Override
	public LotteryDraw fetchResult(String phase) {
		return null;
	}

	@Override
	protected String getResultDetailUrl(String phase) {
		String lotteryId = lotteryTypeMap.get(this.getLotteryType());
		if (lotteryId == null) {
			return null;
		}
		String r = Math.random() + "";
		try {
			return String.format(URL_DEFAULT, lotteryId, phase, r);
		} catch (Exception e) {
			logger.error("从期号中解析日期不正确, phase={}", phase);
			return null;
		}

	}

	@Override
	protected String getResultUrl(String phase) {
		return null;
	}
	
	public abstract LotteryDraw fetchResultDetail(String phase);
	
	protected abstract LotteryDraw parseFrequentLotteryDraw(String jsonData, LotteryDraw lotteryDraw, String encoding, String phase);
}